layui.define(['jquery', 'layer'], function (exports) {
    "use strict";

    let $ = layui.jquery;
    let layer = layui.layer;
    let MOD_NAME = 'http';
    let context = new function () {

        this.login_tips = function () {
            layer.confirm('系统监测到您还没有登录', {title: '登录提醒', icon: 2, closeBtn: 0, btn: ['确定']}, function (index) {
                layui.http.login_out();
            })
        }

        this.login_out = function () {
            this.SendAjax('/common/component/login_out/', [], [], function (res) {
                let login_source = localStorage.getItem('login_source') || '';
                let pathname = '';
                if (login_source == '') {
                    pathname = layui.http.get_pathname();
                } else {
                    pathname = '/' + login_source;
                }
                console.log(pathname);
                localStorage.setItem("token", '');
                localStorage.setItem("userinfo", '');
                localStorage.setItem("login_source", '');
                localStorage.setItem("source", '');
                window.open(pathname + "/index/login/", '_top');
            })
        }


        this.get_pathname = function () {
            let pathname = window.location.pathname || '';
            if (pathname) {
                let str = (pathname.substring(pathname.length - 1) == '/') ? pathname.substring(0, pathname.length - 1) : pathname;
                pathname = str + '/';
            }
            return pathname;
        }
        this.SendAjax = function (url, data = [], headers = [], callback) {
            layer.load(1);
            let token = headers.token || localStorage.getItem('token');
            if (token) {
                headers.token = token;
            }
            let async = true;
            if (data.async) {
                async = false;
            }
            $.ajax({
                url: url,
                type: 'post',
                dataType: 'json',
                data: data,
                headers: headers,
                async: async,
                success: function (res) {
                    layui.layer.closeAll('loading');
                    var code = res.code || 0;
                    var data = res.data || [];
                    var token = data.token || '';
                    if (token) {
                        let userinfo = data.userinfo || {};
                        let login_source = userinfo.login_source || 'admin';
                        localStorage.setItem("login_source", login_source);
                        localStorage.setItem("token", token);
                        localStorage.setItem("userinfo", JSON.stringify(userinfo));
                    }
                    switch (Number(code)) {
                        case 201:
                            layui.http.login_tips();
                            break;

                        default:
                            callback(res)
                    }
                },
                error: function (e) {
                    layui.layer.closeAll('loading');
                    layer.msg('操作失败,请联系技术人员', {icon: 2});
                }
            });
        }
    }
    exports(MOD_NAME, context);
});